import { _decorator, Component, Node, Label, RichText, Vec2, tween, Vec3, director, UITransform } from 'cc';
import { UIBase } from "../UIBase";

const { ccclass, property } = _decorator;

@ccclass('UIAlert')
export class UIAlert extends UIBase {
    
    @property(Node)
    public btnOk: Node = null;

    @property(Node)
    public btnCancel: Node = null;

    @property(Label)
    public txtTitle: Label = null;

    @property(RichText)
    public txtMsg: RichText = null;

    @property(Node)
    public panel: Node = null;

    private title: string = "提示";
    private okText: string = "确定";
    private cancelText: string = "取消";
    private showCancel: boolean = true;
    private defaultColor: string = "#948510";

    protected onLoad(): void {
        this.btnOk.on(Node.EventType.TOUCH_END, this.onOk, this);
        this.btnCancel.on(Node.EventType.TOUCH_END, this.onCancal, this);
    }

    start() {

    }

    protected onEnable(): void {
        super.onEnable();
        this.node.setPosition(new Vec3(0, 0, 0));
        this.zoom(this.panel, "zoomOut");
    }

    public setOkText(text: string) {
        this.okText = text;
        this.btnOk.getComponentInChildren(Label).string = text;
        return this;
    }

    public setCancelText(text: string) {
        this.cancelText = text;
        if (this.btnCancel.active) {
            this.btnCancel.getComponentInChildren(Label).string = text;
        }
        return this;
    }

    public setShowCancel(show: boolean) {
        this.showCancel = show;
        this.btnCancel.active = this.showCancel;
        return this;
    }

    public setTitle(title: string) {
        this.title = title || this.title || "提示";
        this.txtTitle.string = title;
        return this;
    }

    public setMsg(msg: string) {
        if (!msg.startsWith("<color=#")) {
            msg = `<color=${this.defaultColor}>${msg}</color>`;
        }
        this.txtMsg.string = msg;
        return this;
    }

    private onOk() {
        this.node.emit("ok");
        this.close();
    }

    private onCancal() {
        this.node.emit("cancel");
        this.close();
    }

    private close() {
        this.node.active = false;
    }
}